<?php

namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;


/**
 * @OA\Info(
 *     title="PS_RD项目API文档",
 *     version="1.0",
 *     @OA\Contact(
 *          email="675666296@qq.com"
 *      )
 * )
 */
class UserController extends Controller
{
    protected $userModel;

    public function __construct(){
        $this->userModel = new User();
    }

    /**
     * @OA\Post(
     *     tags={"用户信息"},
     *     path="/client/user/onLogin",
     *     summary="服务端实现小程序登录（获取openid）",
     *     description="通过code值获取小程序用户的openid和session_key",
     *     @OA\Parameter(
     *          name="code",
     *          description="登录时获取的 code，可通过wx.login获取",
     *          in="query",
     *          required=true,
     *          @OA\Schema(
     *              type="string"
     *          )
     *     ),
     *     @OA\Response(
     *          response=200,
     *          description="code合法，成功返回openid和session_key等信息",
     *           @OA\JsonContent(
     *              type="object",
     *              @OA\Property(property="code", type="number",description="状态码",default="200"),
     *              @OA\Property(property="msg", type="string",description="提示信息",default="openid已成功返回"),
     *              @OA\Property(property="data", type="object",
     *                  @OA\Property(property="openid", type="string"),
     *                  @OA\Property(property="session_key", type="string"),
     *              ),
     *           )
     *           )
     *     )
     * )
     */
    public function onLogin(Request $request){
        //获取code参数
        $code = $request->input('code');

        //组装GET请求的URL链接
        $arr=[
            'appid' => config('wechat.mini_program.AppID'),
            'secret' => config('wechat.mini_program.AppSecret'),
            'js_code' => $code,
            'grant_type' => 'authorization_code'
        ];

        $url = 'https://api.weixin.qq.com/sns/jscode2session?'.http_build_query($arr);

        //请求curl模拟Get请求微信小程序服务端接口jscode2session，并获取信息
        $res = $this->getReqByCurl($url);

        if(array_key_exists('errcode',$res) && $res['errcode'] !== 0){
            return $this->error($res['errcode'],$res['errmsg']);
        }

        //成功响应后将用户的openid信息存入数据库
        $data = [
          'openid' => $res['openid'],
          'session_key'=>$res['session_key']
        ];

        $this->userModel->saveData('openid',$res['openid'],$data);

        return $this->success($data,"openid已成功返回");

    }

    public function userInfo(Request $request){
        $openid = $request->input('openid');

        $res = $this->userModel->getInfo('openid', $openid);

        return $this->success($res);
    }

    /**
     * 更新用户的真实姓名
     * @param Request $request
     * @return mixed
     */
    public function updateTrueName(Request $request){

        $trueName = $request->input('trueName');
        $openid = $request->input('openid');
        $data = [
            'true_name' => $trueName
        ];

        $res = $this->userModel->saveData('openid',$openid,$data);

        return $this->success($res,'更新记录成功');

    }

    /**
     * 更新用户的手机号码
     * @param Request $request
     * @return mixed
     */
    public function updatePhone(Request $request){
        $phone = $request->input('phone');
        $openid = $request->input('openid');
        $data = [
            'phone' => $phone
        ];

        $res = $this->userModel->saveData('openid',$openid,$data);

        return $this->success($res,'更新记录成功');

    }

    public function updateAvatarUrl(Request $request){
        $openid = $request->input('openid');
        $avatarUrl = $request->input('avatarUrl');
        $data = [
            'avatarUrl' => $avatarUrl
        ];
        $res = $this->userModel->saveData('openid',$openid,$data);

        return $this->success($res,'更新记录成功');
    }

    public function  updateGender(Request $request){
        $openid = $request->input('openid');
        $gender = $request->input('gender');
        $data = [
            'gender' => $gender
        ];
        $res = $this->userModel->saveData('openid',$openid,$data);
        return $this->success($res,'更新记录成功');
    }



}
